package com.qf.servlet;

import com.qf.entity.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @date 2024/6/26
 * @desc 注册
 */
@WebServlet("/regist")
public class RegistServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 0 设置编码格式
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");

        // 利用对象封装数据,传递给Service
        User user = new User( );
        user.setUsername(username);
        user.setPassword(password);
        user.setPhone(phone);
        user.setMoney(0); // 初始余额,根据实际开发情况设置
        user.setSex(Integer.parseInt(sex));
        user.setCreateTime(new Date(  ));// 注册时间根据当前时间确定
        boolean regist = userService.regist(user);

        PrintWriter writer = resp.getWriter( );
        if (regist) {
            writer.write("<html>");
            writer.write("<head>");
            writer.write("<title>注册成功</title>");
            writer.write("</head>");
            writer.write("<body>");
            writer.write("<h1 style='color:green'>注册成功</h1>");
            writer.write("</body>");
            writer.write("</html>");
        } else {
            writer.write("<html>");
            writer.write("<head>");
            writer.write("<title>注册失败</title>");
            writer.write("</head>");
            writer.write("<body>");
            writer.write("<h1 style='color:red'>注册失败</h1>");
            writer.write("</body>");
            writer.write("</html>");
        }


    }
}
